package cn.chengpengper.seckill.stock.application.dubbo;

import cn.chengpengper.dubbo.interfaces.stock.SeckillStockDubboService;
import cn.chengpengper.seckill.common.cache.model.SeckillBusinessCache;
import cn.chengpengper.seckill.common.code.HttpCode;
import cn.chengpengper.seckill.common.exception.SeckillException;
import cn.chengpengper.seckill.common.model.dto.SeckillStockDTO;
import cn.chengpengper.seckill.stock.application.service.SeckillStockBucketService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author chengpeng.hu
 */
@Component
@DubboService(version = "1.0.0")
public class SeckillStockStockDubboServiceImpl implements SeckillStockDubboService {

    @Autowired
    private SeckillStockBucketService seckillStockBucketService;

    @Override
    public SeckillBusinessCache<Integer> getAvailableStock(Long goodsId, Long version) {
        if(Objects.isNull(goodsId)){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillStockBucketService.getAvailableStock(goodsId,version);
    }

    @Override
    public SeckillBusinessCache<SeckillStockDTO> getSeckillStock(Long goodsId, Long version) {
        if (Objects.isNull(goodsId)){
            throw new SeckillException(HttpCode.PARAMS_INVALID);
        }
        return seckillStockBucketService.getSeckillStock(goodsId, version);

    }
}
